package client.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import client.RenrenClient;
import entity.TextInfo;

public class MyJPanel extends JPanel {

	private static final long serialVersionUID = 6753452634479824793L;

	private int type;

	private long UID;
	private String name;

	JScrollPane jsp;

	JFormattedTextField newsfeedField;
	JButton pubButton;

	public MyJPanel(int _type, long _uid, String _name) {
		UID = _uid;
		type = _type;
		name = _name;
		this.setBounds(0, 0, MainWindow.WIDTH, MainWindow.HEIGHT);
		this.setLayout(null);
		this.setVisible(true);
	}

	public void refresh() {
		this.removeAll();
		switch (type) {
		case MainWindow.HOME:
			newsfeedField = new JFormattedTextField();
			newsfeedField.setBounds(10, 10, 105, 25);
			this.add(newsfeedField);
			pubButton = new JButton("发布");
			pubButton.setBounds(125, 10, 60, 25);
			pubButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					try {
						if (RenrenClient.client.publishNewsFeed(UID,
								newsfeedField.getText(), 0))
							JOptionPane.showMessageDialog(null, "发布成功", "提示信息",
									JOptionPane.INFORMATION_MESSAGE);
					} catch (HeadlessException e) {
						e.printStackTrace();
					}
					newsfeedField.setText(null);
				}
			});
			this.add(pubButton);

			Vector<TextInfo> vector = new Vector<TextInfo>();
			vector = RenrenClient.client.checkNewsFeed(UID);

			JPanel newslist = new JPanel();
			newslist.setLayout(null);
			if (vector != null) {
				for (int i = 0; i < vector.size(); i++) {
					TextInfo tmpInfo = vector.elementAt(i);
					JLabel name_label = new JLabel(tmpInfo.getName());
					name_label.setName(new Integer((int) tmpInfo.getUID())
							.toString());
					name_label.setForeground(MainWindow.NAME_COLOR);

					JLabel time_label = new JLabel(tmpInfo.getDatetime()
							.toString());
					time_label.setForeground(MainWindow.NAME_COLOR);

					JLabel newsfeed_label = new JLabel(tmpInfo.getText());
					newsfeed_label.setName(new Integer((int) tmpInfo.getTID())
							.toString());
					newsfeed_label.setForeground(MainWindow.NEWS_COLOR);

					name_label.setBounds(5, i * 50, 80, 20);
					name_label.setFont(new Font("楷体_GB2312", Font.ITALIC, 15));
					newslist.add(name_label);

					time_label.setBounds(90, i * 50, 100, 20);
					time_label.setFont(new Font("楷体_GB2312", Font.ITALIC, 8));
					newslist.add(time_label);

					newsfeed_label.setBounds(15, i * 50 + 25, 200, 20);
					newsfeed_label.setFont(new Font("微软雅黑", Font.PLAIN, 13));
					newslist.add(newsfeed_label);
					newslist.setPreferredSize(new Dimension(200, i * 50));
				}
			}
			newslist.setVisible(true);

			JScrollPane jsp = new JScrollPane(
					ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
					ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			jsp.getViewport().add(newslist);
			jsp.setBounds(12, 50, 173, 250);
			jsp.setVisible(true);
			this.add(jsp);

			break;

		case MainWindow.FRIENDS:
			FriendsPane friends_pane = new FriendsPane(SwingConstants.TOP,
					JTabbedPane.SCROLL_TAB_LAYOUT, UID, name);
			this.add(friends_pane);
			break;
		case MainWindow.RECENT:
			break;
		case MainWindow.MESSAGE:
			break;
		case MainWindow.REMIND:
			break;
		}

	}

	public long getUID() {
		return UID;
	}

}
